import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mall/constant/app_colors.dart';
import 'package:mall/gen/r.dart';
import 'package:mall/modules/model/goods_detail_entity.dart';
import 'package:mall/modules/service/goods_service.dart';
import 'package:mall/modules/service/user_service.dart';
import 'package:mall/router/navigator_util.dart';
import 'package:mall/ui/page/goods/detail/widgets/goods_detail_select_goods_widget.dart';
import 'package:mall/utils/image_component/image_component.dart';

class GoodsDetailBottomBarWidget extends StatefulWidget {
  const GoodsDetailBottomBarWidget({super.key, required this.model});

  final GoodsDetailEntity model;
  @override
  State<GoodsDetailBottomBarWidget> createState() => _GoodsDetailBottomBarWidgetState();
}

class _GoodsDetailBottomBarWidgetState extends State<GoodsDetailBottomBarWidget> {
  RxInt specificationId = 0.obs;

  @override
  Widget build(BuildContext context) {
    return BottomAppBar(
      height: 60,
      padding: EdgeInsets.zero,
      color: Colors.white,
      child: Container(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            SizedBox(width: 50),
            //收藏
            Container(
              width: 50,
              child: InkWell(
                onTap: () => _collection(widget.model),
                child: Center(
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      ImageComponent(
                        R.image.detailCollect.path,
                        size: 22,
                      ),
                      SizedBox(height: 5),
                      Text(
                        '收藏',
                        style: TextStyle(fontSize: 11, color: AppColors.C_999999),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            //客服
            Container(
              width: 50,
              child: InkWell(
                onTap: () => _collection(widget.model),
                child: Center(
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      ImageComponent(
                        R.image.detailKefu.path,
                        size: 22,
                      ),
                      SizedBox(height: 5),
                      Text(
                        '客服',
                        style: TextStyle(fontSize: 11, color: AppColors.C_999999),
                      ),
                    ],
                  ),
                ),
              ),
            ),

            //购买或者添加购物车
            ClipRRect(
                borderRadius: BorderRadius.circular(22.0),
                child: Container(
                  height: 44,
                  width: 180,
                  child: Row(
                    children: [
                      Expanded(
                        flex: 1,
                        child: InkWell(
                          onTap: () => openBottomSheet(context, widget.model, 1),
                          child: Container(
                            color: Color(0xff342627),
                            child: Center(
                                child: Text(
                              '加入购物车',
                              style: TextStyle(fontSize: 13, color: AppColors.C_FFFFFF),
                            )),
                          ),
                        ),
                      ),
                      Expanded(
                        flex: 1,
                        child: InkWell(
                          onTap: () => openBottomSheet(context, widget.model, 2),
                          child: Container(
                            color: Color(0xffff3874),
                            child: Center(
                                child: Text(
                              '直接购买',
                              style: TextStyle(fontSize: 13, color: AppColors.C_FFFFFF),
                            )),
                          ),
                        ),
                      ),
                    ],
                  ),
                )),
          ],
        ),
      ),
    );
  }

  openBottomSheet(BuildContext context, GoodsDetailEntity model, int showType) {
    Get.bottomSheet(
      backgroundColor: Colors.white,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(15),
          topRight: Radius.circular(15),
        ),
      ),
      GoodsDetailSelectGoodsWidget(model: model,showType: showType,),
    );
  }

  

  _collection(GoodsDetailEntity model) async {
    final islogin = await UserService.isLogin();
    if (!islogin) {
      NavigatorUtil.goLogin();
      return;
    }
    ;

    GoodsService.addOrDeleteCollect(model.info?.id ?? 0);
  }


}
